package xml;

/**
 * Created with IntelliJ IDEA.
 * User: Dimas
 * Date: 19.02.13
 * Time: 19:30
 * To change this template use File | Settings | File Templates.
 */
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

public class ReadXML {
    protected Element eElement;
    public void readFile(){
        try {
            File fXmlFile = new File("/Users/valeriykuchma/IdeaProjects/TradExpert/tradExpert/src/config.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);

            doc.getDocumentElement().normalize();
            NodeList nList = doc.getElementsByTagName("database");

            for (int temp = 0; temp < nList.getLength(); temp++) {

                Node nNode = nList.item(temp);

                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    eElement = (Element) nNode;
                }
            }

        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public String getConfigData(String field){
        return  eElement.getElementsByTagName(field).item(0).getTextContent();
    }



}
